<template>
  <div class="pb-select-tag ">
    <div class="flex items-center">
      <div class="select-box ">
        <div v-if="selectList.length > 0" class="flex ">
          <span v-for="(item, index) in selectList" :key="index" class="tag-css mr-1"
            style="background: var(--pb-color-primary-light-1);">{{ item[defaultProps.label]
            }}</span>
        </div>
        <p class="placeholder " :class="isNull ? 'danger' : ''" v-else>
          请选择
        </p>
      </div>

      <el-popover :width="300" trigger="click" placement="bottom" @show="showSelect = true" @hide="showSelect = false">
        <template #reference>
          <div style="width:25px ;height: 100%;cursor: pointer;" @click.stop="showSelect = !showSelect">
            <pb-icon name="small-bottom" size="16" :class="isNull ? 'danger' : ''"></pb-icon>
          </div>
        </template>
        <div class="select-card ">
          <el-input v-model="filterVal" placeholder="可输入搜索" @input="filterMethod" class="input-css" />
          <div class="tag-box">
            <p v-for="(tag, index) in optionsData" @click="clickHandler(tag, index)" :key="index" class="tag-item"
              :class="tag.checked ? 'tag-active' : ''">{{
                tag[defaultProps.label] }}
            </p>
          </div>
        </div>
      </el-popover>
    </div>
    <div>
    </div>
  </div>
</template>

<script setup lang="ts">

/**
 * @param List 标签列表
 * @param defaultProps 默认参数
 * @param checkList 已选择的列表
 * @param isSingle 是否是单选
 */
const props = defineProps({
  List: {
    type: Array,
    default: []
  },
  checkList: {
    type: Array,
    default: []
  },
  defaultProps: {
    type: Object,
    default: {
      label: 'label',
      value: 'value'
    }
  },
  isSingle: {
    type: Boolean,
    default: false
  }
})

const emits = defineEmits(['change'])
// 判断用户是否没有选择
const isNull = ref(false)
const showSelect = ref(false)
watch(
  showSelect,
  (val) => {
    if (val) {
      isNull.value = false
      backList.value.forEach(el => {
        el.checked = false
        props.checkList.forEach(it => {
          if (el[props.defaultProps.value] == it) {
            el.checked = true
          }
        })
      })
      optionsData.value = backList.value
    } else {
      showSelect.value = false
      filterVal.value = ''
      isNull.value = selectList.value.length == 0 ? true : false
      emits('change', selectList.value)
    }
  }
)

watch(
  () => props.checkList,
  (val) => {
    formatFn();
  }
)


const filterVal = ref('')

const backList = ref(props.List)
const optionsData = ref([])

// 用户选择的标签
const selectList = ref([])


// 给数据添加状态
const formatFn = () => {
  selectList.value = [];
  backList.value.forEach(el => {
    el.checked = false
    props.checkList.forEach(it => {
      if (el[props.defaultProps.value] == it) {
        el.checked = true
        selectList.value.push(el)
      }
    })
  })
  optionsData.value = backList.value
}



// 筛选方法
const filterMethod = (value) => {
  if (value) {
    optionsData.value = [...props.List].filter((item) =>
      item[props.defaultProps.label].includes(value)
    );
  } else {
    optionsData.value = props.List
  }
};

// 选择标签
const clickHandler = (tag, idx) => {
  // 单选做排他
  if (props.isSingle) {
    props.List.forEach((el, index) => {
      el.checked = false
      if (el[props.defaultProps.value] == tag[props.defaultProps.value]) {
        el.checked = true
        selectList.value.splice(0, 1, el)
      }
    })
  } else {
    tag.checked = !tag.checked
    let flag = selectList.value.findIndex(el => el[props.defaultProps.value] == tag[props.defaultProps.value])
    if (flag == -1) {
      selectList.value.push(tag)
    } else {
      selectList.value.splice(flag, 1)
    }
  }
}

onMounted(() => {
  formatFn()
})
</script>

<style lang="scss" scoped>
:v-deep .el-input__inner {
  border-radius: 0px;
  border-top-width: 0px;
  border-left-width: 0px;
  border-right-width: 0px;
  border-bottom-width: 1px;
  /*outline: medium;*/
}

.select-box {
  max-width: 87%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tag-css {
  padding: 2px 5px;
  border-radius: 2px;
  border: 1px solid var(--pb-color-primary-light-5);
  color: var(--pb-color-primary);
  font-weight: 400;
  font-size: 14px;
  line-height: 20px;
  white-space: nowrap;
}


.placeholder {
  display: inline-block;
  width: 64px;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.25);
  line-height: 22px;
}

.danger {
  color: var(--pb-color-danger);
}

.select-card {
  min-width: 150px;
  max-width: 300px;

}

.tag-box {
  display: flex;
  flex-wrap: wrap;
  padding: 8px 0;
  margin-right: -8px;

  .tag-item {
    @extend.tag-css;
    cursor: pointer;
    margin: 0 8px 8px 0;

    &:not(:nth-child(even)):last-child {
      margin-right: 0;
    }
  }

  .tag-active {
    background: var(--pb-color-primary);
    color: #FFFFFF;
    border: none;
  }
}
</style>